package com.caimiao.miao.interfaces;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by cachowu on 16/9/6.
 */
public abstract class IBaseAdapter<T> extends BaseAdapter {
    protected LayoutInflater mInflater;
    protected Context mContext;
    protected List<T> mDataList;
    protected View.OnClickListener mOnBtnClickListener;

    protected abstract int getItemLayout();

    protected abstract BaseHolder createHolder(View convertView);

    protected abstract void setLogic(int position, View convertView, BaseHolder holder);

    public IBaseAdapter(Context context, T[] array) {
        this(context, new ArrayList<T>());
        mDataList = new ArrayList<>();
        int length = array == null ? 0 : array.length;
        for (int i = 0; i < length; i++) {
            mDataList.add(array[i]);
        }
    }

    public IBaseAdapter(Context context, List<T> dataLists) {
        mContext = context;
        mDataList = dataLists;
        mInflater = LayoutInflater.from(context);
    }

    public void setOnBtnClickListener(View.OnClickListener onClickListener) {
        mOnBtnClickListener = onClickListener;
    }

    public List<T> getData() {
        return mDataList;
    }

    public void setUpdate(List<T> dataLists, boolean isRefresh) {
        if (isRefresh) setRefresh(dataLists);
        else setMore(dataLists);
    }

    public void setRefresh(List<T> dataLists) {
        mDataList = dataLists;
        notifyDataSetChanged();
    }

    public void setMore(List<T> dataLists) {
        if (dataLists == null) return;
        if (mDataList == null) mDataList = new ArrayList<>();
        mDataList.addAll(dataLists);
        notifyDataSetChanged();
    }

    public void add(T data) {
        if (mDataList == null) mDataList = new ArrayList<>();
        if (data != null) {
            mDataList.add(data);
            notifyDataSetChanged();
        }
    }

    public void add(int location, T data) {
        if (mDataList == null) mDataList = new ArrayList<>();
        if (data != null && (location >= 0 && location <= mDataList.size())) {
            mDataList.add(location, data);
            notifyDataSetChanged();
        }
    }

    public void remove(T data) {
        if (data != null) {
            mDataList.remove(data);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        if (mDataList != null)
            return mDataList.size();
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mDataList != null && position >= 0 && position < getCount())
            return mDataList.get(position);
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final BaseHolder mHolder;
        if (convertView == null) {
            convertView = mInflater.inflate(getItemLayout(), parent, false);
            mHolder = createHolder(convertView);
            convertView.setTag(mHolder);
        } else {
            mHolder = (BaseHolder) convertView.getTag();
        }
        setLogic(position, convertView, mHolder);
        return convertView;
    }

    protected class BaseHolder {
        public View convertView;
        private HashMap<Integer, View> mViewMap = new HashMap<>();

        public BaseHolder(View convertView) {
            this.convertView = convertView;
        }

        public <T> T getView(Integer viewId) {
            View view = mViewMap.get(viewId);
            if (view == null) {
                view = convertView.findViewById(viewId);
                mViewMap.put(viewId, view);
            }
            return (T) view;
        }

        public void setText(Integer viewId, String value) {
            TextView textView = getView(viewId);
            if (textView != null) textView.setText(value);
        }

        public void setText(Integer viewId, int valueId) {
            TextView textView = getView(viewId);
            if (textView != null) textView.setText(valueId);
        }
    }
}
